//
//  CategoryGoupView.m
//  JiaoX
//
//  Created by 尹桥印 on 14/11/17.
//  Copyright (c) 2014年 北京娇羞科技有限公司. All rights reserved.
//

#import "CategoryGroupView.h"
#import "UIImageView+WebCache.h"


@interface CategoryGroupView()
@property(nonatomic,weak) UIImageView *groupIcon;
@property(nonatomic,weak) UILabel *groupTitle;
@property(nonatomic,weak) UIImageView *acceView;
@property(nonatomic,weak) UIButton *button;
@end


@implementation CategoryGroupView

-(id)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if(self!=nil){
        
        //添加一个btn，用于点击效果
        UIButton *button = [[UIButton alloc]init];
        [button setBackgroundImage:[UIImage imageWithColor:[UIColor whiteColor]] forState:UIControlStateNormal];
        [button setBackgroundImage:[UIImage imageWithColor:JXColor(204, 204, 204)] forState:UIControlStateHighlighted];
        button.frame = CGRectMake(0, 0, JXScreenW, CateGroupViewHeight);
        [button addTarget:self action:@selector(buttonClick) forControlEvents:UIControlEventTouchUpInside];
        _button = button;
        [self addSubview:button];
        
        //添加图标
        UIImageView *groupIcon = [[UIImageView alloc]init];
        
        groupIcon.frame = CGRectMake(10, 15, 18, 18);
        _groupIcon = groupIcon;
        
        [self addSubview:groupIcon];
        
        //添加右边指示器
        
        CGFloat acceViewHeight = 14;
        CGFloat acceViewWidth = 14;
        CGFloat acceViewX = JXScreenW-JXPublicInset-acceViewWidth;
        CGFloat acceViewY = (CateGroupViewHeight-acceViewHeight)/2;
        
        UIImageView *acceView = [[UIImageView alloc]init];
        acceView.frame = CGRectMake(acceViewX, acceViewY, acceViewWidth, acceViewHeight);
        acceView.image = [UIImage imageNamed:@"category_acce_open"];
        _acceView = acceView;
        [self addSubview:acceView];
        
        //添加label
        UILabel *groupTitle = [[UILabel alloc]init];
        groupTitle.textColor = JXColor(77, 77, 77);
        groupTitle.font = [UIFont systemFontOfSize:16];
        CGFloat titleX = CGRectGetMaxX(groupIcon.frame)+JXPublicInset/2;
        CGFloat titleY = (CateGroupViewHeight-16)/2;
        CGFloat titleW = JXScreenW-titleX-(JXScreenW-CGRectGetMinX(acceView.frame))-JXPublicInset;
        groupTitle.frame = CGRectMake(titleX, titleY, titleW, 16);
        _groupTitle = groupTitle;
        
        [self addSubview:groupTitle];
        
        self.backgroundColor = [UIColor whiteColor];
        
    }
    return self;
}

-(void)buttonClick{
    // 通知代理
    if ([self.btnClickDelegate respondsToSelector:@selector(categoryGroupViewClick:)]) {
        [self.btnClickDelegate categoryGroupViewClick:self.section];
    }
}

/**
 *  分类gruop的view
 *
 *  @param icon  icon的url
 *  @param title 标题
 *
 *  @return
 */
+(instancetype)initWithIcon:(NSString *)icon title:(NSString *)title status:(BOOL)status{
    CategoryGroupView *view = [[self alloc]init];
    view.groupTitle.text = title;
    [view.groupIcon sd_setImageWithURL:[NSURL URLWithString:icon]];
    NSString *image = @"category_acce_open";
    if (status) {
        image = @"category_acce_close";
    }
    [view.acceView setImage:[UIImage imageNamed:image]];
    return view;
}

@end
